package com.lee.www.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lee.www.Mapper.PermissionMapper;
import com.lee.www.entity.Permission;
import com.lee.www.vo.CommonResult;
import com.lee.www.vo.MsgInfo;
import com.lee.www.vo.Permission2Vo;
import com.lee.www.vo.PermissionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionMapper permissionMapper;

    @GetMapping("/list")
    public List<Permission> list() {
        return permissionMapper.selectList(null);
    }

    @PostMapping()
    public void insert(Permission permission) {
        permissionMapper.insert(permission);
    }

    @GetMapping("/vo/list")
    public CommonResult voList() {
        List<Permission> permissions = permissionMapper.selectList(null);
        List<Permission2Vo> permissionVos = new ArrayList<>();
        for (Permission permission : permissions) {
            Permission2Vo permissionVo = new Permission2Vo();
            BeanUtils.copyProperties(permission, permissionVo);
            permissionVos.add(permissionVo);
        }
        MsgInfo msgInfo = MsgInfo.initMsgInfo(200L, "数据获取成功");
        return CommonResult.initCommonResult(permissionVos, msgInfo);
    }

    // 构建两级的Tree型数据结构
    @GetMapping("/vo/tree")
    public CommonResult voTree() {
        // 使用QueryWrapper进行构造查询条件
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 0L);
        List<Permission> permissions = permissionMapper.selectList(queryWrapper);

        List<PermissionVo> permissionVos = new ArrayList<>();
        for (Permission permission : permissions) {
            PermissionVo permissionVo = new PermissionVo();
            BeanUtils.copyProperties(permission, permissionVo);
            List<Permission2Vo> permission2Vos = new ArrayList<>();
            QueryWrapper<Permission> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2
                    .eq("pid", permission.getId());
            List<Permission> permissions2 = permissionMapper.selectList(queryWrapper2);
            for (Permission permissionTmp : permissions2) {
                Permission2Vo permission2Vo = new Permission2Vo();
                BeanUtils.copyProperties(permissionTmp, permission2Vo);
                permission2Vos.add(permission2Vo);
            }
            permissionVo.setChildren(permission2Vos);
            permissionVos.add(permissionVo);
        }
        MsgInfo msgInfo = MsgInfo.initMsgInfo(200L, "数据获取成功");
        return CommonResult.initCommonResult(permissionVos, msgInfo);
    }
}
